package com.jichaoyun.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jichaoyun.common.model.PagedResult;
import com.jichaoyun.model.entity.StationInfo;


import com.jichaoyun.model.entity.UpdownLimit;
import com.jichaoyun.model.vo.UpdownLimitVo;
import com.jichaoyun.sys.mapper.StationInfoMapper;
import com.jichaoyun.sys.mapper.UpDownLimitMapper;
import com.jichaoyun.sys.service.UpdownLimitService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UpdownLimitServiceImpl extends ServiceImpl<UpDownLimitMapper, UpdownLimit>
implements UpdownLimitService {
    @Autowired
    private UpDownLimitMapper upDownLimitMapper;

    @Autowired
    private StationInfoMapper stationInfoMapper;


    @Override
    public PagedResult pageSelectAll(Integer pageNo) {
        IPage<UpdownLimit> page = new Page<>(pageNo, 10);
        LambdaQueryWrapper<UpdownLimit> wp = new LambdaQueryWrapper<>();
        wp.orderByDesc(UpdownLimit::getBeginTime);
        IPage<UpdownLimit> page1 = upDownLimitMapper.selectPage(page, wp);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setRows(page1.getRecords());
        pagedResult.setPages(page1.getPages());
        pagedResult.setTotal(page1.getTotal());
        return pagedResult;
    }

    @Override
    public PagedResult pageSelectOne(Integer pageNo, Integer stationNo) {
        IPage<UpdownLimit> page = new Page<>(pageNo, 10);
        LambdaQueryWrapper<UpdownLimit> wp = new LambdaQueryWrapper<>();
        wp.eq(UpdownLimit::getStationNo,stationNo)
          .orderByDesc(UpdownLimit::getBeginTime);
        IPage<UpdownLimit> page1 = upDownLimitMapper.selectPage(page, wp);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setRows(page1.getRecords());
        pagedResult.setPages(page1.getPages());
        pagedResult.setTotal(page1.getTotal());
        return pagedResult;
    }

    @Override
    public PagedResult pageSelectReg(Integer pageNo) {
        //分页查询
        IPage<StationInfo> page = new Page<>(pageNo, 10);
        LambdaQueryWrapper<StationInfo> wp = new LambdaQueryWrapper<>();
        wp.orderByAsc(StationInfo::getStationNo);
        IPage<StationInfo> page1 = stationInfoMapper.selectPage(page, wp);
        PagedResult pagedResult = new PagedResult();
        List<StationInfo> records = page1.getRecords();
        ArrayList<UpdownLimitVo> updownLimitVos = new ArrayList<>();
        //这里对上下限进行回显
        for (StationInfo record : records) {
            UpdownLimitVo updownLimitVo = new UpdownLimitVo();
            updownLimitVo.setDownFlow(record.getDownFlow());
            updownLimitVo.setUpFlow(record.getUpFlow());
            updownLimitVo.setStationNo(record.getStationNo());
            updownLimitVo.setStationName(record.getStationName());
            updownLimitVos.add(updownLimitVo);
        }
        pagedResult.setRows(updownLimitVos);
        pagedResult.setPages(page1.getPages());
        pagedResult.setTotal(page1.getTotal());
        return pagedResult;
    }

    @Override
    public void setOneStation(UpdownLimitVo updownLimitVo) {
        //插入启动记录
        UpdownLimit updownLimit = new UpdownLimit();
        BeanUtils.copyProperties(updownLimitVo,updownLimit);
        updownLimit.setInsertTime(LocalDateTime.now());
        updownLimit.setIsBegin(false);
        upDownLimitMapper.insert(updownLimit);

    }

    @Override
    public void setStations(List<UpdownLimitVo> updownLimitVos) {
        for (UpdownLimitVo updownLimitVo : updownLimitVos) {
            //插入启动记录
            UpdownLimit updownLimit = new UpdownLimit();
            BeanUtils.copyProperties(updownLimitVo,updownLimit);
            updownLimit.setInsertTime(LocalDateTime.now());
            updownLimit.setIsBegin(false);
            upDownLimitMapper.insert(updownLimit);
        }
    }
}
